<template>
  <div>
    <el-table :data="permissionArr" style="width: 100%; margin-bottom: 20px" row-key="id" border>
      <el-table-column label="名称" prop="name" />
      <el-table-column label="权限值" prop="code" />
      <el-table-column label="修改时间" prop="updateTime" />
      <el-table-column label="操作" width="250">
        <template #default="{ row }">
          <el-button v-has="'btn.Permission.add'" type="primary" size="small" :disabled="row.level === 4 ? true : false" @click="addPermission(row)">添加{{ row.level === 3 ? '功能' : '菜单' }}</el-button>
          <el-button v-has="'btn.Permission.update'" type="primary" size="small" :disabled="row.level === 1 ? true : false" @click="updatePermission(row)">编辑</el-button>
          <el-popconfirm :title="`你确定要删除${row.name}吗？`" width="260" @confirm="removePermission(row)">
            <template #reference>
              <el-button v-has="'btn.Permission.remove'" type="danger" size="small" :disabled="row.level === 1 ? true : false">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog v-model="dialogVisible" :title="menuData.id ? '更新菜单' : '添加菜单'" width="500">
      <el-form label-width="80px" :inline="false">
        <el-form-item label="名称">
          <el-input v-model="menuData.name" placeholder="请你输入菜单名称" />
        </el-form-item>
        <el-form-item label="权限值">
          <el-input v-model="menuData.code" placeholder="请你输入权限数值" />
        </el-form-item>
      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="save">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { getAllPermission, reqAddOrUpdateMenu, reqDeleteMenu } from '@/api/acl/menu';
import type { PermissionData, PermissionList, Permission, MenuParams } from '@/api/acl/menu/type.ts';
import { ElMessage } from 'element-plus';
import { onMounted, reactive, ref } from 'vue';

onMounted(() => {
  getHasPremission();
});

//菜单权限列表
let permissionArr = ref<PermissionList>([]);

//获取权限列表
const getHasPremission = async () => {
  let result: PermissionData = await getAllPermission();
  if (result.code === 200) {
    permissionArr.value = result.data;
  }
};

//控制对话框显示与隐藏
let dialogVisible = ref<boolean>(false);

// 携带参数
let menuData = reactive<MenuParams>({
  code: '',
  level: 0,
  name: '',
  pid: 0,
});

//添加菜单
const addPermission = async (row: Permission) => {
  dialogVisible.value = true;
  Object.assign(menuData, {
    code: '',
    level: 0,
    name: '',
    pid: 0,
    id: 0,
  });
  menuData.pid = row.id as number;
  menuData.level = row.level + 1;
};

//编辑菜单
const updatePermission = async (row: Permission) => {
  dialogVisible.value = true;
  Object.assign(menuData, row);
};

//删除菜单
const removePermission = async (row: Permission) => {
  let result: any = await reqDeleteMenu(row.id as number);
  if (result.code === 200) {
    ElMessage({
      type: 'success',
      message: '删除成功',
    });
    getHasPremission();
  } else {
    ElMessage({
      type: 'error',
      message: '删除失败',
    });
  }
};

//确定按钮的回调
const save = async () => {
  let result: any = await reqAddOrUpdateMenu(menuData);
  if (result.code === 200) {
    dialogVisible.value = false;
    ElMessage({
      type: 'success',
      message: menuData.id ? '编辑成功' : '添加成功',
    });
    getHasPremission();
  } else {
    ElMessage({
      type: 'error',
      message: menuData.id ? '编辑失败' : '添加失败',
    });
  }
};
</script>

<style lang="scss" scoped></style>
